package com.ray.life;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

//import android.util.Log;

public class RSSHandler extends DefaultHandler {
//    private final String TAG = "RSSHandler";

    private boolean isInItem = false;
//    private boolean isInTitle = false;
//    private boolean isInLink = false;
//    private boolean isInDesc = false;
//    private boolean isInDate = false;
    private boolean isInMainTitle = false;
    private ArrayList<News> mList;
    private News news;
    private String mTitle = "";
    private StringBuffer buf = new StringBuffer();

    @Override
    public void startDocument() throws SAXException {
        mList = new ArrayList<News>();
    }

    @Override
    public void endDocument() throws SAXException {
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equals("item")) {
            this.isInItem = true;
            news = new News();
        } else if (localName.equals("title")) {
            if (this.isInItem) {
//                this.isInTitle = true;
            } else {
                this.isInMainTitle = true;
            }
        } else if (localName.equals("link")) {
            if (this.isInItem) {
//                this.isInLink = true;
            }
        } else if (localName.equals("description")) {
            if (this.isInItem) {
//                this.isInDesc = true;
            }
        } else if (localName.equals("pubDate")) {
            if (this.isInItem) {
//                this.isInDate = true;
            }
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (localName.equals("item")) {
            this.isInItem = false;
            mList.add(news);
        } else if (localName.equals("title")) {
            if (this.isInItem) {
                news.setTitle(buf.toString().trim());
                buf.setLength(0);
//                this.isInTitle = false;
            } else {
                mTitle = buf.toString().trim();
                buf.setLength(0);
                this.isInMainTitle = false;
            }
        } else if (localName.equals("link")) {
            if (this.isInItem) {
                news.setLink(buf.toString().trim());
                buf.setLength(0);
//                this.isInLink = false;
            }
        } else if (localName.equals("description")) {
            if (isInItem) {
                news.setDesc(buf.toString().trim());
                buf.setLength(0);
//                this.isInDesc = false;
            }
        } else if (localName.equals("pubDate")) {
            if (isInItem) {
                String date = buf.toString().trim();
//                Log.e(TAG, "date = " + date);
                news.setDate(date);
                buf.setLength(0);
//                this.isInDate = false;
            }
        }
    }

    @Override
    public void characters(char ch[], int start, int length) {
        if (this.isInItem || this.isInMainTitle) {
            buf.append(ch, start, length);
        }
    }

    public ArrayList<News> getParsedData() {
        return mList;
    }

    public String getRssTitle() {
        return mTitle;
    }
}
